package com.objecteffects.template.web.action;

import java.io.IOException;

import com.google.inject.Inject;

import com.objecteffects.template.tumblr.TumblrResponse;
import com.objecteffects.template.tumblr.method.Likes;
import com.objecteffects.template.tumblr.method.Likes.LikesResponse.LikedPost;
import com.objecteffects.template.util.logger.InjectLogger;
import com.objecteffects.template.web.utils.LoginRequired;
import com.objecteffects.template.web.utils.TumblrAccessToken;

import org.scribe.model.Token;

import org.slf4j.Logger;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;

/**
 * @author Rusty Wright
 */
@LoginRequired
public class LikesActionBean extends BaseActionBean {
    @InjectLogger
    private Logger log;

    private static final String METHOD = "likes";

    private final TumblrAccessToken tumblrAccessToken;

    @Inject
    LikesActionBean(final TumblrAccessToken _tumblrAccessToken) {
        this.tumblrAccessToken = _tumblrAccessToken;
    }

    @DefaultHandler
    public Resolution startHandler() throws IOException {
        final String tumblrUrl = this.getContext().getTumblrUrl();

        if (tumblrUrl == null) {
            this.log.debug("tumblrUrl is null");

            this.getContext().getMessages()
            .add(new SimpleMessage("Tumblr url is null."));

            return new RedirectResolution(TumblrStartUrlActionBean.class);
        }

        final Token accessToken =
            this.tumblrAccessToken.fetchToken(tumblrUrl, this.getContext()
                                              .getUserId());

        if (accessToken == null) {
            this.log.debug("accessToken: {}", accessToken);

            return new RedirectResolution(TumblrStartUrlActionBean.class);
        }

        final TumblrResponse<Likes> built =
            new TumblrResponse.Builder<Likes>().accessToken(accessToken)
            .blog(tumblrUrl).method(LikesActionBean.METHOD)
                .type(Likes.class).build();

        final Likes likes = built.responseMapper();

        if (likes == null) {
            this.getContext().getMessages()
            .add(new SimpleMessage("Permission denied."));

            return new ForwardResolution(TemplateView.ERROR);
        }

        this.getContext()
        .getMessages()
        .add(new SimpleMessage(String.format("likes meta: %s",
                                             likes.getMeta())));

        this.getContext()
        .getMessages()
        .add(new SimpleMessage(String.format("likes count: %s", likes
                                             .getResponse().getLikedCount())));

        int i = 0;

        for (final LikedPost post : likes.getResponse().getLikedPosts()) {
            this.getContext().getMessages()
            .add(new SimpleMessage(String.format("liked post: %s", post)));

            if (i++ > 5) {
                break;
            }
        }

        return new ForwardResolution(TemplateView.MESSAGES);
    }
}
